package com.zeyi.chat.controller;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.dto.Chat;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.StageManager;
import com.zeyi.chat.manager.UserManager;
import com.zeyi.chat.po.ChatGroup;
import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.AvatarUtil;
import com.zeyi.chat.utils.FXMLUtil;
import com.zeyi.chat.utils.SocketUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.hamcrest.core.Is;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeyi
 */
public class ChatInfoController {
    @FXML
    private ImageView changeIcon;

    @FXML
    private HBox signatureContainer;

    @FXML
    private HBox addressContainer;

    @FXML
    private Label chatNameTitle;

    @FXML
    private Label chatName;

    @FXML
    private Label accountTitle;

    @FXML
    private Label account;

    @FXML
    private Label signature;

    @FXML
    private Label address;

    @FXML
    private ImageView editIcon;

    @FXML
    private ImageView avatar;

    @FXML
    private ImageView closeIcon;

    private static ChatInfoController instance;

    private Stage editStage;
    private User user;
    private ChatGroup group;

    @FXML
    private void initialize() {
        instance = this;
    }

    public static ChatInfoController getInstance() {
        return instance;
    }

    @FXML
    public void onClose(MouseEvent mouseEvent) {
        Stage stage = (Stage) closeIcon.getScene().getWindow();
        stage.close();
    }

    /**
     * 改变头像
     */
    @FXML
    public void changeAvatar(MouseEvent mouseEvent) {
        String type = accountTitle.getText().equals("账号") ? "0" : "1";
        File tmpFile = AvatarUtil.chooseAvatar((Stage) avatar.getScene().getWindow(), avatar,
                type.equals("0") ? "user" : "group");
        if (tmpFile != null) {
            String path = AvatarUtil.copyAvatar(tmpFile, type.equals("0") ? "user" : "group");
            Integer id = Integer.valueOf(closeIcon.getScene().getRoot().getId());
            if (type.equals("0")) {
                HomeController.getInstance().updateMyAvatar(AvatarUtil.suffix + path);
                UserManager.getCurrentUser().setAvatar(path);
                SocketUtil.send(new Message("ChatHandler/updateUserInfo",
                        JSON.toJSONString(new User(id, path))));
            } else {
                HomeController.getInstance().updateGroup(new ChatGroup(id, null, AvatarUtil.suffix + path));
                List<Chat> chatList = UserManager.getCurrentUser().getChatList();
                for (int i = 0; i < chatList.size(); i++) {
                    Chat chat = chatList.get(i);
                    if (chat.getCreateId() != null && chat.getId().equals(id)) {
                        chat.setAvatar(path);
                        break;
                    }
                }
                SocketUtil.send(new Message("ChatHandler/updateGroupInfo",
                        JSON.toJSONString(new ChatGroup(id, null, path))));
            }
        }
    }

    /**
     * 编辑信息
     */
    @FXML
    public void editInfo(MouseEvent mouseEvent) {
        if (editStage != null) editStage.close();
        if (user != null) {
            editStage = FXMLUtil.createStage("/Fxml/EditUserInfo.fxml");
            EditUserInfoController.getInstance().initData();
        } else {
            editStage = FXMLUtil.createStage("/Fxml/EditGroupInfo.fxml");
            EditGroupInfoController.getInstance().initData(group);
        }
    }

    /**
     * 展示用户信息
     */
    public void displayUserInfo(User user, boolean changeable) {
        this.user = user;
        String signature = user.getPersonalSignature();
        String address = user.getAddress();
        this.signature.setText(signature == null || signature.equals("") ? "-" : signature);
        this.address.setText(address == null || address.equals("") ? "-" : address);
        display(user.getAvatar(), "昵称", user.getUsername(), "账号", user.getEmail(),
                changeable, "0");
    }

    /**
     * 展示群信息
     */
    public void displayGroupInfo(ChatGroup group, boolean changeable) {
        this.group = group;
        display(group.getAvatar(), "群名", group.getGroupName(), "群号", group.getGroupCode(),
                changeable, "1");
    }

    private void display(String avatarUrl, String chatNameTitle, String chatName,
                         String accountTitle, String account, boolean changeable, String type) {
        avatar.setImage(new Image(AvatarUtil.suffix + avatarUrl, 400, 400, false, true));
        if (changeable) {
            editIcon.setVisible(true);
            editIcon.setDisable(false);
            changeIcon.setVisible(true);
            changeIcon.setDisable(false);
        } else {
            editIcon.setVisible(false);
            editIcon.setDisable(true);
            changeIcon.setVisible(false);
            changeIcon.setDisable(true);
        }
        this.chatNameTitle.setText(chatNameTitle);
        this.chatName.setText(chatName);
        this.accountTitle.setText(accountTitle);
        this.account.setText(account);
        if (type.equals("0")) {
            addressContainer.setVisible(true);
            signatureContainer.setVisible(true);
        } else {
            addressContainer.setVisible(false);
            signatureContainer.setVisible(false);
        }
    }
}
